﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using ChoViet.Lib.Common;

namespace ChoViet.Model
{
    public class UserManager
    {
        public static MembershipUser GetMember(string userName)
        {
            try
            {
                ChoViet.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new ChoViet.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                MembershipUser u = a.Get(userName);
                return u;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }

        public static MembershipUser GetMember(object userNameorKey, bool iskey)
        {
            try
            {
                ChoViet.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new ChoViet.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                MembershipUser u = iskey ? a.Get(userNameorKey) : GetMember(userNameorKey.ToString());
                if (u == null) return GetMember("admin");
                return u;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static MembershipUser GetUser(object userNameorKey, bool iskey)
        {
            try
            {
                ChoViet.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new ChoViet.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                MembershipUser u = iskey ? a.Get(userNameorKey) : GetMember(userNameorKey.ToString());
                if (u == null) return null;
                return u;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static Guid GetProviderUserKey(string userName)
        {
            try
            {
                ChoViet.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new ChoViet.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                MembershipUser u = a.Get(userName);
                return (Guid)u.ProviderUserKey;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static bool CheckRole(string userName, string role)
        {
            try
            {
                return true;
                //ChoViet.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new ChoViet.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                //MembershipUser u = a.Get(userName);
                //ChoViet.Lib.MvcMembership.AspNetRoleProviderWrapper r = new ChoViet.Lib.MvcMembership.AspNetRoleProviderWrapper(Roles.Provider);
                //if (r != null && u != null)
                //{
                //    if (r.IsInRole(u, RoleSystem.Administrator)) return true;
                //    else
                //        return r.IsInRole(u, role);
                //}
                //else return false;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static bool CheckRole(string role)
        {
            string userName = HttpContext.Current.User.Identity.Name;
            return CheckRole(userName, role);
        }
        public static Guid GetUserId()
        {
            MembershipUser myObject = Membership.GetUser();
            return new Guid(myObject.ProviderUserKey.ToString());
        }
        public static MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            MembershipCreateStatus status;
            Membership.Provider.CreateUser(userName, password, email, null, null, true, null, out status);
            return status;

        }
        public static bool CheckByEmail(string email)
        {
            try
            {
                var u= Membership.GetUserNameByEmail(email);
                return (u != null && u != string.Empty) ? true : false;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static bool CheckUserName(string userName)
        {
            try
            {
                var u = Membership.GetUser(userName);
                return u != null;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static MembershipUser GetByEmail(string email)
        {
            try
            {
                ChoViet.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new ChoViet.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                var l = a.FindByEmail(email, 0, 10);
                return l.Count > 0 ? l.FirstOrDefault() : null;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        
    }
}
